// pages/store/store.js
Page({
  onGoodsCardTap(e){
    // 获取点击商品索引
    // 获取当前侧边栏选中分类索引
    const index = e.currentTarget.dataset.goodIndex;
    const goodsData = this.data.goodCard[index]; // 获取完整商品数据

    // 存储到全局变量
    const app = getApp()
    app.globalData.selectedGoods = goodsData

    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail'
    });
  },
  onMilkTeaCartTap(e){
    if (this.data.goodsCart.totalNum > 0) {
      this.setData({
        cardVisible: true,
      });
    }
  },
  onCardVisibleChange(e){
    this.setData({
      cardVisible: e.detail.visible,
    });
  },
  onClearTap(e){
    this.setData({
      goodsCart: {
        totalNum: 0,
        totalPrice: 0,
        goods: []
      },
      cardVisible: false
    });
  },
  onCartStepperChange(e){
    const index = e.currentTarget.dataset.goodsIndex; // 获取商品索引
    const value = e.detail.value; // 获取新数值
    const goods = this.data.goodsCart.goods;

    // 创建新数组避免直接修改原数据
    const newGoods = [...goods];
    const changedItem = {...newGoods[index]};
    // 计算价格差异
    const priceDiff = (value - changedItem.selectedNum) * changedItem.selectedPrice;

    if (value ===0) {
      // 删除商品逻辑
      newGoods.splice(index, 1); // 从数组中移除该商品
      this.setData({
        goodsCart: {
          totalNum: this.data.goodsCart.totalNum - changedItem.selectedNum,
          totalPrice: Number((this.data.goodsCart.totalPrice - changedItem.totalPrice).toFixed(2)),
          goods: newGoods
        }
      });
    }
    else {
      // 更新商品数量
      changedItem.selectedNum = value;
      changedItem.totalPrice = value * changedItem.selectedPrice;

      // 更新数组
      newGoods[index] = changedItem;

      this.setData({
        goodsCart: {
          totalNum: Math.max(this.data.goodsCart.totalNum + (value - changedItem.selectedNum), 0),
          totalPrice: Number((Number(this.data.goodsCart.totalPrice) + priceDiff).toFixed(2)),
          goods: newGoods
        }
      });
    }

    if (this.data.goodsCart.totalNum === 0) {
      this.setData({
        cardVisible: false,
      });
    }
  },
  onMilkTeaOrderSettlementTap(e){
    if (this.data.goodsCart.totalNum === 0) {
      wx.showToast({ title: '请先添加商品', icon: 'none' });
      return;
    }
    getApp().globalData.goodsOrder = this.data.goodsCart;

    wx.navigateTo({
      url: '/pages/goodsOrderSettlement/goodsOrderSettlement',
    });
  },
  /**
   * 页面的初始数据
   */
  data: {
    goodsCart:[],
    cardVisible: false,
    banners:[
      {
        "id": "1",
        "value": "https://tdesign.gtimg.com/mobile/demos/swiper1.png"
      },
      {
        "id": "2",
        "value": "https://tdesign.gtimg.com/mobile/demos/swiper1.png"
      }
    ],
    goodCard:[
      {
        id: 1,
        title: '标题',
        content: '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容',
        img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
        price: 100,
        specification:[
          {
            name: '规格',
            selectedIndex:0,
            options:[
              {
                title: '大',
                additional_price: 120.1,
              },
              {
                title: '中',
                additional_price: 110
              },
              {
                title: "小",
                additional_price: 100
              }
            ]
          },
          {
            name: '状态',
            selectedIndex:0,
            options:[
              {
                title: '热',
                additional_price: 0,
              },
              {
                title: '温',
                additional_price: 0
              },
              {
                title: "冰",
                additional_price: 0
              }
            ]
          },
          {
            name: '甜度',
            selectedIndex:0,
            options:[
              {
                title: '标准',
                additional_price: 0
              },
              {
                title: '少糖',
                additional_price: 0
              },
              {
                title: "不加糖",
                additional_price: 0
              }
            ]
          }
        ],
        imgList: [
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png'
        ],
        goodsDetailImg:[
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png'
        ]
      },
      {
        id: 2,
        title: '标题',
        content: '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容',
        img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
        price: 100,
        specification:[
          {
            name: '规格',
            selectedIndex:0,
            options:[
              {
                title: '大',
                additional_price: 120.1,
              },
              {
                title: '中',
                additional_price: 110
              },
              {
                title: "小",
                additional_price: 100
              }
            ]
          },
          {
            name: '状态',
            selectedIndex:0,
            options:[
              {
                title: '热',
                additional_price: 0,
              },
              {
                title: '温',
                additional_price: 0
              },
              {
                title: "冰",
                additional_price: 0
              }
            ]
          },
          {
            name: '甜度',
            selectedIndex:0,
            options:[
              {
                title: '标准',
                additional_price: 0
              },
              {
                title: '少糖',
                additional_price: 0
              },
              {
                title: "不加糖",
                additional_price: 0
              }
            ]
          }
        ],
        imgList: [
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png'
        ],
        goodsDetailImg:[
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png'
        ]
      },
      {
        id: 3,
        title: '标题',
        content: '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容',
        img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
        price: 100,
        specification:[
          {
            name: '规格',
            selectedIndex:0,
            options:[
              {
                title: '大',
                additional_price: 120.1,
              },
              {
                title: '中',
                additional_price: 110
              },
              {
                title: "小",
                additional_price: 100
              }
            ]
          },
          {
            name: '状态',
            selectedIndex:0,
            options:[
              {
                title: '热',
                additional_price: 0,
              },
              {
                title: '温',
                additional_price: 0
              },
              {
                title: "冰",
                additional_price: 0
              }
            ]
          },
          {
            name: '甜度',
            selectedIndex:0,
            options:[
              {
                title: '标准',
                additional_price: 0
              },
              {
                title: '少糖',
                additional_price: 0
              },
              {
                title: "不加糖",
                additional_price: 0
              }
            ]
          }
        ],
        imgList: [
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png'
        ],
        goodsDetailImg:[
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
          'https://tdesign.gtimg.com/mobile/demos/swiper1.png'
        ]
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app = getApp();
    if (app.globalData.goodsCart===null){
      app.globalData.goodsCart = {totalNum: 0,totalPrice: 0,goods: []}
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const app = getApp();
    if (app.globalData.goodsCart===null){
      app.globalData.goodsCart = {totalNum: 0,totalPrice: 0,goods: []}
    }else {
      this.setData({
        goodsCart :app.globalData.goodsCart
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})